package models

import (
	"blog_api/db"
)

type Power struct {
	Id         int    `gorm:"column(id)" json:"id"`
	Level      int    `gorm:"column(level)" json:"level"`
	ParentId   int    `gorm:"column(parent_id)" json:"parent_id"`
	Icon       string `gorm:"column(icon)" json:"icon"`
	Name       string `gorm:"column(name)" json:"name"`
	Path       string `gorm:"column(path)" json:"path"`
	Desc       string `gorm:"column(desc)" json:"desc"`
	Order      int    `gorm:"column(order)" json:"order"`
	IsShow     int    `gorm:"column(is_show)" json:"is_show"`
	CreateTime uint64 `gorm:"column(create_time)" json:"create_time"`
	UpdateTime uint64 `gorm:"column(update_time)" json:"update_time"`
}

func (*Power) TableName() string {
	return "blog_power"
}

/**
 * PowerTree
 * @Description: 后台的左侧菜单tree
 * @Author: Jazk-Z
 * @return []map[string]interface{}
 */
func PowerTree() []map[string]interface{} {
	var pp []Power
	db.Mysql.Model(&Power{}).Where("is_show = ? and parent_id = ?", 1, 0).Order("`order`").Find(&pp)

	var cp []Power
	db.Mysql.Model(&Power{}).Where("is_show = ? and level = ?", 1, 2).Order("`order`").Find(&cp)

	var data []map[string]interface{}
	for _, v := range pp {
		item := make(map[string]interface{})
		item["id"] = v.Id
		item["level"] = v.Level
		item["icon"] = v.Icon
		item["name"] = v.Name
		item["path"] = v.Path
		var child []map[string]interface{}

		for _, vv := range cp {
			if vv.ParentId == v.Id {
				citem := make(map[string]interface{})
				citem["id"] = vv.Id
				citem["level"] = vv.Level
				citem["icon"] = vv.Icon
				citem["name"] = vv.Name
				citem["path"] = vv.Path
				child = append(child, citem)
			}
		}
		item["child"] = child

		data = append(data, item)
	}

	return data
}

func AdminPowerList(limit, offset int, keyword string) {

}
